
using System; 
using System.Collections.Generic;
using UnityEngine;
using XFABManager;



namespace XFGameFramework
{

    // Fix 编码

    public class UpdateManager : MonoBehaviour
    {

        #region 静态字段

        private static UpdateManager _instance;

        #endregion

        #region 静态属性

        private static UpdateManager Instance 
        {

            get 
            {
                if (!_instance && ApplicationTool.isPlaying) 
                { 
                    GameObject obj = new GameObject("UpdateManager");
                    DontDestroyOnLoad(obj);
                    _instance = obj.AddComponent<UpdateManager>();
                }
                 
                return _instance;
            }

        }

        #endregion

        #region 事件

        private Action _update;
        private Action _fixedUpdte;
        private Action _lateUpdate;


        [Obsolete("已过时,请使用方法UpdateManager.AddUpdateEvent来注册Update事件!", true)]
        public static Action onUpdate;
 
        [Obsolete("已过时,请使用方法UpdateManager.AddFixedUpdateEvent来注册Update事件!", true)]
        public static Action onFixedUpdate;
 
        [Obsolete("已过时,请使用方法UpdateManager.AddLateUpdateEvent来注册Update事件!", true)]
        public static Action onLateUpdate;  

        #endregion
 

        private void FixedUpdate()
        {
            _fixedUpdte?.Invoke();
        }

        private void Update()
        {
            _update?.Invoke();
        }

        private void LateUpdate()
        {
           _lateUpdate?.Invoke();
        }


        public static void AddUpdateEvent(Action action) 
        {
            if (Instance) 
            {
                Instance._update += action;
            }
        }

        public static void AddFixedUpdateEvent(Action action) 
        {
            if (Instance)
            {
                Instance._fixedUpdte += action;
            }
        }

        public static void AddLateUpdateEvent(Action action) 
        {
            if (Instance)
            {
                Instance._lateUpdate += action;
            }
        }

        public static void RemoveUpdateEvent(Action action) 
        {
            if (Instance)
            {
                Instance._update -= action;
            }
        }

        public static void RemoveFixedUpdateEvent(Action action)
        {
            if (Instance)
            {
                Instance._fixedUpdte -= action;
            }
        }

        public static void RemoveLateUpdateEvent(Action action)
        {
            if (Instance)
            {
                Instance._lateUpdate -= action;
            }
        }
         
    }
}

